import { ComputeOptions, Options, PipelineOperator, redact } from "../../core";
import { Iterator } from "../../lazy";
import { AnyObject } from "../../types";
import { AsyncIterator } from "../../async/lazy";
/**
 * Restricts the contents of the documents based on information stored in the documents themselves.
 *
 * See {@link https://docs.mongodb.com/manual/reference/operator/aggregation/redact/ usage}
 */
export const $redact: PipelineOperator = (
  collection: Iterator | AsyncIterator,
  expr: AnyObject,
  options: Options
): Iterator | Promise<AsyncIterator> => {
  const copts = ComputeOptions.init(options);
  if (options.isAsync) {
    return Promise.resolve(collection as AsyncIterator).then(async (collection) => {
      return collection.map((obj: AnyObject) =>
        redact(obj, expr, copts.update(obj))
      );
    });
  }
  return (collection as Iterator).map((obj: AnyObject) =>
    redact(obj, expr, copts.update(obj))
  );
};
